package com.ming.learn.from.carl.doublepointer;

import com.ming.commons.utils.ListNode;

/*
第206题：反转链表
 */
public class Of20201229LeetCode206 {
    /*
    题意：反转一个单链表。
    示例: 输入: 1->2->3->4->5->NULL
    输出: 5->4->3->2->1->NULL
     */
    public static void main(String[] args) {
        ListNode listNode = reverseList(new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4)))));
        while (listNode!=null){
            System.out.print(listNode.val+"\t");
            listNode=listNode.next;
        }
    }
    private static ListNode reverseList(ListNode head) {
        ListNode newHead = null;
        ListNode temp;
        while (head!=null){
            temp=head;
            head=head.next;
            temp.next=newHead;
            newHead=temp;
        }
        return newHead;
    }
}
